package com.tgu.home_action.service.listener.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.tgu.home_action.dao.VolunteerInformationMapper;
import com.tgu.home_action.pojo.VolunteerInformation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

// 有个很重要的点 DemoDataListener 不能被spring管理，要每次读取excel都要new,然后里面用到spring可以构造方法传进去
public class VolunteerInformationListener extends AnalysisEventListener<VolunteerInformation> {
    private static final Logger LOGGER = LoggerFactory.getLogger(VolunteerInformationListener.class);
    /**
     * 每隔5条存储数据库，实际使用中可以3000条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 5;
    List<VolunteerInformation> list = new ArrayList<VolunteerInformation>();
    List<VolunteerInformation> failList = new ArrayList<VolunteerInformation>();

    public List<VolunteerInformation> getFailList() {
        return failList;
    }

    private final VolunteerInformationMapper volunteerInformationMapper;


    public VolunteerInformationListener(VolunteerInformationMapper volunteerInformationMapper) {
        this.volunteerInformationMapper = volunteerInformationMapper;
    }

    /**
     * 这个每一条数据解析都会来调用
     */
    @Override
    public void invoke(VolunteerInformation volunteerInformation, AnalysisContext context) {
        if (volunteerInformation.getGender() == null) {
            volunteerInformation.setSex((short) 0);
        } else if (volunteerInformation.getGender().equals("男")) {
            volunteerInformation.setSex((short) 1);
        } else if (volunteerInformation.getGender().equals("女")) {
            volunteerInformation.setSex((short) 2);
        } else if (volunteerInformation.getGender().equals("1")) {
            volunteerInformation.setSex((short) 1);
        } else if (volunteerInformation.getGender().equals("2")) {
            volunteerInformation.setSex((short) 2);
        } else {
            volunteerInformation.setSex((short) 0);
        }
        list.add(volunteerInformation);
        // 达到BATCH_COUNT了，需要去存储一次数据库，防止数据几万条数据在内存，容易OOM
        if (list.size() >= BATCH_COUNT) {
            saveData();
            // 存储完成清理 list
            list.clear();
        }
    }

    /**
     * 所有数据解析完成了 都会来调用
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        // 这里也要保存数据，确保最后遗留的数据也存储到数据库
        saveData();
        LOGGER.info("所有数据解析完成！");
    }

    /**
     * 加上存储数据库
     */
    private void saveData() {
        if (list.size() > 0) {
            Boolean isSuccessAll = null;
            try {
                isSuccessAll = volunteerInformationMapper.batchInsertVolunteer(list);
            } catch (Exception e) {
                isSuccessAll = false;
            }
            if (!isSuccessAll) {
                for (VolunteerInformation volunteerInformation : list) {
                    Boolean isSuccess = null;
                    try {
                        isSuccess = volunteerInformationMapper.insertVolunteer(volunteerInformation);
                    } catch (Exception e) {
                        isSuccess = false;
                    }
                    if (!isSuccess) {
                        failList.add(volunteerInformation);
                    }
                }
            }
        }

    }
}